package com.koron.css2.workflow;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.workflow.bean.ApprovalCommentConfigBean;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 审批意见配置修改
 */
public class ApprovalCommentConfigUpdate implements ServerInterface {

    private static Logger logger = LogManager.getLogger(ApprovalCommentConfigList.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", String.class);
        try {
            ApprovalCommentConfigBean bean = JsonUtils.objectToPojo(req.getData(), ApprovalCommentConfigBean.class);
            if (StringUtils.isEmpty(bean.getSetCode())){
                info.setCode(Constant.MESSAGE_INT_FAIL);
                info.setDescription("setCode 不能为空");
                return info;
            }
            if (StringUtils.isEmpty(bean.getProcessCode())){
                info.setCode(Constant.MESSAGE_INT_FAIL);
                info.setDescription("processCode 不能为空");
                return info;
            }
            if (StringUtils.isEmpty(bean.getTaskCode())){
                info.setCode(Constant.MESSAGE_INT_FAIL);
                info.setDescription("taskCode 不能为空");
                return info;
            }
            if (StringUtils.isEmpty(bean.getComment())){
                info.setCode(Constant.MESSAGE_INT_FAIL);
                info.setDescription("comment 不能为空");
                return info;
            }
            bean.setCreateUser(userInfo.getUserInfo().getAcount());
            return WorkflowImpl.insertApprovalCommentConfig(bean);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return info;
    }
}
